<?php


namespace app\admin\freight\controller;


use app\admin\freight\logic\FreightLogic;
use app\admin\freight\validate\FreightValidate;
use app\common\controller\BaseAdminController;

/**
 * 运费模板
 * Class FreightController
 * @package app\admin\freight\controller
 */
class FreightController extends BaseAdminController
{
    protected function init()
    {
        parent::init();
        $this->logic = new FreightLogic();
        $this->validate = new FreightValidate();
    }

    public function add()
    {
        $data = request()->post();
        $this->beforeAddOrEdit("add", $data);
        if ($this->validate) {
            if (!$this->validate->scene('add')->check($data)) {
                return $this->error($this->validate->getError());
            }
        }
        $this->logic->transaction(function () use ($data) {
            $this->logic->addFreight($data);
        });
        return $this->success();
    }

    public function update()
    {
        $data = request()->post();
        $this->beforeAddOrEdit("update", $data);
        if ($this->validate) {
            if (!$this->validate->scene('update')->check($data)) {
                return $this->error($this->validate->getError());
            }
        }
        $this->logic->transaction(function () use ($data) {
            $this->logic->editFreight($data);
        });
        return $this->success();
    }

    public function read()
    {
        $data = $this->logic->readFreight(input('id'));
        return $this->success($data);
    }

    public function getAll()
    {
        $data = $this->logic->getList([
            'status' => 1,
        ], "*", 'weigh desc');
        return $this->success($data);
    }
}